package com.song.Niodemo01;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Future;

public class NIO2001 {

	
	
	public static void testbuffer()
	{
		ByteBuffer bb = ByteBuffer.allocate(48);
		
		bb.put(new byte[]{1,2,4,2,-13});
		
		while(bb.hasRemaining())
		{
			System.out.println(bb.get());
		}
	}
	
	public static void charbuffer()
	{
		ByteBuffer bb = ByteBuffer.allocate(1024);
		bb.asCharBuffer().put("helloword");
		bb.limit("helloword".length() * 2);
		
		while(bb.hasRemaining())
		{
			System.out.println(bb.getChar());
		}
	}
	
	public static void intbuffer()
	{
		ByteBuffer bb = ByteBuffer.allocate(1024);
		IntBuffer ib = bb.asIntBuffer();
		ib.put(new int[]{2,32,12,-12});
		
		System.out.println("bytebufer.postion  " +bb.position());
		System.out.println("bytebufer.limit  " +bb.limit());
		System.out.println("bytebufer.capacity  " +bb.capacity());
		System.out.println("bytebufer.postion  " +ib.position());
		System.out.println("bytebufer.limit  " +ib.limit());
		System.out.println("bytebufer.capacity  " +ib.capacity());
	
		while(ib.hasRemaining())
		{
			System.out.print(ib.get()+"  \t  ");
		}
		System.out.println();
		ib.flip();
		while(ib.hasRemaining())
		{
			System.out.print(ib.get() +"  \t  ");
		}
	}
	
	
	public static void testChannel()
	{
		try {
			FileChannel inChannel = FileChannel.open(Paths.get("C:\\Documents and Settings\\Administrator\\桌面\\data"), StandardOpenOption.READ);
			FileChannel outchannel = FileChannel.open(Paths.get("cc"), StandardOpenOption.WRITE);
			
			ByteBuffer buf = ByteBuffer.allocate(48);
			
			int bytesread = inChannel.read(buf);
			while(bytesread != -1)
			{
				buf.flip();
				
				while(buf.hasRemaining())
				{
					outchannel.write(buf);
				}
				buf.clear();
				bytesread = inChannel.read(buf);
				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void testTransferChannel()
	{
		try {
			FileChannel inChannel = FileChannel.open(Paths.get("C:\\Documents and Settings\\Administrator\\桌面\\data"), StandardOpenOption.READ);
			
			FileChannel outchannel = FileChannel.open(Paths.get("cc"), StandardOpenOption.WRITE);
			
			outchannel.transferFrom(inChannel, 0, inChannel.size());
			
//			inChannel.transferTo(0, inChannel.size(), outchannel);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	public static void Test异步讲来式(){
		
		try {
			AsynchronousFileChannel inchannel = AsynchronousFileChannel.open(Paths.get("C:\\Documents and Settings\\Administrator\\桌面\\data"), StandardOpenOption.READ);
			ByteBuffer buffer =ByteBuffer.allocate(1024);
			
			Future<Integer> result = inchannel.read(buffer, 0);
			
			while(!result.isDone())
			{
				
			}
			buffer.flip();
			while(buffer.hasRemaining())
			{
				System.out.print((char) buffer.get());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void Test异步回调()
	{
		try {
			AsynchronousFileChannel inchannel = AsynchronousFileChannel.open(Paths.get("C:\\Documents and Settings\\Administrator\\桌面\\data"), StandardOpenOption.READ);
			
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			
			inchannel.read(buffer, 0, buffer,new CompletionHandler<Integer, ByteBuffer>() {

				public void completed(Integer result, ByteBuffer attachment) {
					// TODO Auto-generated method stub
					
				}

				public void failed(Throwable exc, ByteBuffer attachment) {
					// TODO Auto-generated method stub
					
				}
			});
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void testServerChannel()
	{
		try {
			ServerSocketChannel serverchannel = ServerSocketChannel.open();
			serverchannel.configureBlocking(false);
			serverchannel.socket().bind(new InetSocketAddress(9999));
			ByteBuffer bb = ByteBuffer.allocate(1024);
			while(true)
			{
				SocketChannel socketChannel = serverchannel.accept();
				if(socketChannel!=null)
				{
					while(socketChannel.read(bb)==-1)
					{
						
					}
					bb.flip();
					
					while(bb.hasRemaining())
					{
						System.out.print((char)bb.get());
					}
					
				}
				

			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static  void testclientsocketchannel()
	{
		try {
			SocketChannel socketChannel =SocketChannel.open();
			socketChannel.connect(new InetSocketAddress(9999));
			socketChannel.configureBlocking(false);
			
			while(!socketChannel.finishConnect())
			{
				
			}
			ByteBuffer bb = ByteBuffer.allocate(1024);
			bb.put("helloword".getBytes());
			socketChannel.write(bb);
//			while(socketChannel.read(bb)!=-1)
//			{
//				bb.flip();
//				while(bb.hasRemaining())
//				{
//					socketChannel.write(bb);
//				}
//				bb.clear();
//			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public static void main(String[] args) {
//		testServerChannel();
		testclientsocketchannel();
	}
	
}
